package com.example.task1.ui.AllService.volunteer;

import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.example.task1.R;
import com.example.task1.application.MyApplication;
import com.example.task1.bean.VolunteerBean;
import com.example.task1.bean.VolunteerMyBean;
import com.example.task1.http.Api;
import com.example.task1.ui.BaseAdapter;
import com.example.task1.util.ApiResp;
import com.example.task1.util.CallBack;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import retrofit2.Call;

public class VolunteerMyAdapter extends BaseAdapter<VolunteerMyBean.RowsDTO> {

    private Context context;
    private List<VolunteerMyBean.RowsDTO> mData = new ArrayList<>();
    private TextView tvTitle;
    private TextView tvUndertaker;
    private TextView tvRequireText;
    private TextView tvDetail;
    private TextView tvStartAt;
    private Button btnRemove;
    private VolunteerMyActivity activity;

    public VolunteerMyAdapter(VolunteerMyActivity activity) {
        this.activity = activity;
        this.context = activity;

    }
    @Override
    protected void setData(List<VolunteerMyBean.RowsDTO> rows) {
        mData.clear();
        int size = mData.size();
        mData.addAll(rows);
        notifyItemInserted(size);
    }

    @Override
    protected int getLayout() {
        return R.layout.volunteermylist;
    }

    @Override
    protected Context getContext() {
        return null;
    }

    @Override
    protected List<VolunteerMyBean.RowsDTO> getData() {
        return mData;
    }

    @Override
    protected void onBind(myHolder holder, VolunteerMyBean.RowsDTO date) {
        tvDetail.setText("活动详情：" + date.getDetail());
        tvRequireText.setText("报名要求：" + date.getRequireText());
        tvStartAt.setText("----" + date.getStartAt());
        tvTitle.setText(date.getTitle());
        tvUndertaker.setText("承办单位：" + date.getUndertaker());
        btnRemove.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                HashMap<String, Object> map = new HashMap<>();
                map.put("activityId", date.getId());
                map.put("newState", false);
                Call<ApiResp> post = Api.api().post(MyApplication.TOKEN, "/prod-api/api/volunteer-service/register", Api.getBody(map));
                Api.send(post, ApiResp.class, new CallBack<ApiResp>() {
                    @Override
                    public void success(ApiResp data) {
                        MyApplication.show("取消成功");
                        activity.initData();
                    }

                    @Override
                    public void error(String msg) {

                    }
                });
            }
        });
    }

    @Override
    protected void holder(View view) {
        initView(view);
    }

    private void initView(View view) {
        tvTitle = view.findViewById(R.id.tv_title);
        tvUndertaker = view.findViewById(R.id.tv_undertaker);
        tvRequireText = view.findViewById(R.id.tv_requireText);
        tvDetail = view.findViewById(R.id.tv_detail);
        tvStartAt = view.findViewById(R.id.tv_startAt);
        btnRemove = view.findViewById(R.id.btn_remove);
    }
}
